/*
 * testdominion.c
 * CS362 Assignment 4
 * Hugh McDonald (mcdonalh)
 */
#include <stdlib.h>
#include <stdio.h>
#include <time.h>
#include "dominion.h"
#include "dominion_helpers.h"

int main (int argc, char *argv[]) {

	struct gameState *testGame = newGame();
	int i, j, index, num;
	srand(time(NULL));
	int cardList[20] = {remodel, baron, feast, smithy, mine, adventurer, gardens, village, council_room, great_hall, salvager, sea_hag, treasure_map, tribute, ambassador, minion, steward, cutpurse, embargo, outpost};

	// Generate random set of kingdom cards
	printf("Generating random set of kingdom cards\n");
	int randomNumbers[10] = {-1, -1, -1, -1, -1, -1, -1, -1, -1, -1};
	for (i = 0; i < 10; i++) {
		index = i;
		num = rand() % 20;
		for (j = 0; j < 10; j++) {
			if (num == randomNumbers[j]) {
				i--;
				break;
			}
		}
		randomNumbers[index] = num;
	}
	int cards[10] = {cardList[randomNumbers[0]], cardList[randomNumbers[1]], cardList[randomNumbers[2]], cardList[randomNumbers[3]], cardList[randomNumbers[4]], cardList[randomNumbers[5]], cardList[randomNumbers[6]], cardList[randomNumbers[7]], cardList[randomNumbers[8]], cardList[randomNumbers[9]]};

	// Generating random number of players between 2 and 4
	int players = (rand() % 3) + 2;
	printf ("Starting %d player game\n", players);

	// Initializing game
	initializeGame(players, cards, -1, testGame);

	printf("Game initialized\n");

	int money = 0;
	int smithyPos = -1;
	int adventurerPos = -1;
	int numSmithies = 0;
	int numAdventurers = 0;

	while (!isGameOver(testGame)) {
		for (j = 0; j < players; j++) {
			printf("Start player %d's turn\n", whoseTurn(testGame) + 1);
			money = 0;
			smithyPos = -1;
			adventurerPos = -1;
			for (i = 0; i < numHandCards(testGame); i++) {
				if (handCard(i, testGame) == copper)
					money++;
				else if (handCard(i, testGame) == silver)
					money += 2;
				else if (handCard(i, testGame) == gold)
					money += 3;
				else if (handCard(i, testGame) == smithy)
					smithyPos = i;
				else if (handCard(i, testGame) == adventurer)
					adventurerPos = i;
			}

			// Action phase
			printf("Action phase\n");
			num = rand() % 2;
			switch (num) {
				case 0:		
					if (smithyPos != -1) {
						printf("Smithy played from position %d\n", smithyPos); 
						playCard(smithyPos, -1, -1, -1, testGame); 
						money = 0;
						for (i = 0; i < numHandCards(testGame); i++){
							if (handCard(i, testGame) == copper){
								playCard(i, -1, -1, -1, testGame);
								money++;
							} else if (handCard(i, testGame) == silver) {
								playCard(i, -1, -1, -1, testGame);
								money += 2;
							} else if (handCard(i, testGame) == gold) {
								playCard(i, -1, -1, -1, testGame);
								money += 3;
							}
						}
					}
					break;
				case 1:
					if (adventurerPos != -1) {
						printf("1: adventurer played from position %d\n", adventurerPos);
						playCard(adventurerPos, -1, -1, -1, testGame); 
						money = 0;
						for (i = 0; i < numHandCards(testGame); i++) {
							if (handCard(i, testGame) == copper) {
								playCard(i, -1, -1, -1, testGame);
								money++;
							} else if (handCard(i, testGame) == silver) {
								playCard(i, -1, -1, -1, testGame);
								money += 2;
							} else if (handCard(i, testGame) == gold) {
								playCard(i, -1, -1, -1, testGame);
								money += 3;
							}
						}
					}
					break;
			}

			printf("Buy phase\n");
			if (money >= 8) {
				printf("Bought province\n");
				buyCard(province, testGame);
			} else if (money >= 6) {
				printf("Bought gold\n");
				buyCard(gold, testGame);
			} else if ((money >= 4) && (numSmithies < 2)) {
				printf("Bought smithy\n");
				buyCard(smithy, testGame);
				numSmithies++;
			} else if (money >= 3) {
				printf("Bought silver\n");
				buyCard(silver, testGame);
			}
			printf("End of player %d's turn\n", whoseTurn(testGame) + 1);
			endTurn(testGame);
		}

		for (i = 0; i < players; i++) {
			printf("Player %d Score: %d\n", i + 1, scoreFor(i, testGame));
		}
            
	}

	printf("Finished game\n");
	
	return 0;
}